<?php
namespace app\admin\controller;

use app\common\cache\agent\IAgentCache;
use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\agent\IAgentAdminRecommendCache;
use app\common\cache\common\IAreaCache;
use app\common\controller\AdminBaseController;
use app\common\enum\AdminSuper;
use app\common\enum\IsCompany;
use app\common\enum\IsDelete;
use app\common\enum\IsLeader;
use app\common\enum\IsReal;
use app\common\enum\IsAuthentication;
use app\common\enum\AgentAdminStatus;
use app\common\ext\IDb;
use app\common\ext\IFake;
use app\common\ext\IRequest;
use app\common\ext\ILog;

class Agent extends AdminBaseController{
    /**
     * @功能：信贷公司列表
     * @开发者：cxl
     * @return string
     */
    public function agent_list(){
        //接收参数
        $agent_id = IRequest::get('agent_id');
        $agent_name = IRequest::get('agent_name');
        $area = IRequest::get('area');

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }

        //设置查询条件
        $agent_where['ag.is_delete'] = IsDelete::No;
        $agent_where['ag.is_authentication'] = array('neq','null');
        if(!empty($agent_id)) $agent_where['ag.agent_id'] = $agent_id;
        if(!empty($agent_name)) $agent_where['ag.agent_name'] = array("like","%{$agent_name}%");
        if(!empty($province_id)) $agent_where['ag.province_id'] = $province_id;
        if(!empty($city_id)) $agent_where['ag.city_id'] = $city_id;
        $agent_list = IDb::getInstance('agent as ag')
            ->setDbFiled('ag.*,aa.real')
            ->setDbJoin('agent_admin as aa','aa.agent_id = ag.agent_id and aa.is_leader=2','left')
            ->setDbWhere($agent_where)
            ->setDbOrder("ag.agent_id desc")
            ->setDbGroup("ag.agent_id")
            ->pag();


        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //页面赋值
        $this->assign("agent_id",$agent_id);
        $this->assign("agent_name",$agent_name);
        $this->assign("agent_list",$agent_list);
        $this->assign("area",$area);
        $this->assign("area_list",$area_list);
        //渲染模板输出
        return view();
    }

    /**
     * @功能：信贷公司列表
     * @开发者：cxl
     * @return string
     */
    public function agent_not_list(){
        //接收参数
        $agent_id = IRequest::get('agent_id');
        $agent_name = IRequest::get('agent_name');

        //设置查询条件
        $agent_where['ag.is_delete'] = IsDelete::No;
        $agent_where['ag.is_authentication'] = array('EXP','IS null');
        if(!empty($agent_id)) $agent_where['ag.agent_id'] = $agent_id;
        if(!empty($agent_name)) $agent_where['ag.agent_name'] = array("like","%{$agent_name}%");
        $agent_list = IDb::getInstance('agent as ag')
            ->setDbFiled('ag.*,aa.real')
            ->setDbJoin('agent_admin as aa','aa.agent_id = ag.agent_id and aa.is_leader=2','left')
            ->setDbWhere($agent_where)
            ->setDbOrder("ag.agent_id desc")
            ->setDbGroup("ag.agent_id")
            ->pag();

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //页面赋值
        $this->assign("agent_id",$agent_id);
        $this->assign("agent_name",$agent_name);
        $this->assign("agent_list",$agent_list);
        $this->assign("area_list",$area_list);
        //渲染模板输出
        return view();
    }
    /**
     * @功能：信贷公司详情
     * @开发者：cxl
     * @return string
     */
    public function agent_info(){
        //接收参数
        $agent_id = IRequest::get("agent_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //取得信贷公司表
        $agent_info = IAgentCache::getInfo($agent_id);

        //取得代理商信息
        $agent_where['agent_id'] = $agent_id;
        $agent_where['is_leader'] = IsLeader::Yes;
        $agent_admin_info = IDb::getInstance('agent_admin')->setDbWhere($agent_where)->row();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("agent_info",$agent_info);
        $this->assign("agent_admin_info",$agent_admin_info);

        //渲染模板输出
        return view();
    }
    /**
     * @功能：添加信贷公司
     * @开发者：cxl
     * @return string
     */
    public function agent_add(){
        //取得省份
        $area_list = IAreaCache::getAllList(3);

        //页面赋值
        $this->assign("area_list",$area_list);

        //渲染模板输出
        return view();
    }
    /**
     * @功能：编辑信贷公司
     * @开发者：cxl
     * @return string
     */
    public function agent_edit(){
        //接收参数
        $agent_id = IRequest::get("agent_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(3);

        //取得信贷公司信息
        $agent_info = IAgentCache::getInfo($agent_id);
        if(empty($agent_info)){
            error("信贷公司不存在！");
        }

        //取得代理商信息
        $agent_admin_where['agent_id'] = $agent_id;
        $agent_admin_where['is_leader'] = IsLeader::Yes;
        $agent_admin_info = IDb::getInstance('agent_admin')->setDbWhere($agent_admin_where)->row();

        //整理区域数据
        $agent_info['area'] = array($agent_info['province_id'],$agent_info['city_id'],$agent_info['area_id']);

        //页面赋值
        $this->assign("agent_id",$agent_id);
        $this->assign("agent_info",$agent_info);
        $this->assign("agent_admin_info",$agent_admin_info);
        $this->assign("area_list",$area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加信贷公司
     * @开发者：cxl
     * @return string
     */
    public function agent_submit(){
        //接收参数
        $agent_id = IRequest::get("agent_id");
        $agent_admin_id = IRequest::get("agent_admin_id");
        $agent_name = IRequest::get("agent_name");
        $area = IRequest::get("area");
        $address = IRequest::get("address");
        $remark = IRequest::get("remark");
        $real = IRequest::get("real",IRequest::NOT_EMPTY,"负责人姓名不能为空！");
        $phone = IRequest::get("phone",IRequest::MOBILE,"负责人手机号码错误！");

        //判断城市是否正确
        $province_id = 0;
        $city_id = 0;
        $area_id = null;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
            if(!empty($area[2])) $area_id = $area[2]; //取得城市
        }

        //判断是否为空
        if($province_id <= 0 || $city_id <= 0){
            error('请选择所在的省份城市！');
        }

        //开启事物处理
        IDb::dbStartTrans();

        //判断是否为新添加数据
        if(empty($agent_id)) {
            //判断名称是否已经存在
            if(!empty($agent_name)){
                $agent_where = null;
                $agent_where['agent_name'] = $agent_name;
                $agent_count = IDb::getInstance('agent')->setDbWhere($agent_where)->con();
                if ($agent_count > 0) {
                    //事物回滚
                    IDb::dbRollback();

                    //提示错误信息
                    error("您输入的信贷公司名称已经被使用了！");
                }
            }

            //添加信贷公司
            $agent_data = null;
            $agent_data['agent_name'] = $agent_name;
            $agent_data['province_id'] = $province_id;
            $agent_data['city_id'] = $city_id;
            $agent_data['area_id'] = $area_id;
            $agent_data['address'] = $address;
            $agent_data['remark'] = $remark;
            $agent_data['create_time'] = get_date_time();
            $agent_data['is_delete'] = IsDelete::No;
            $agent_add = IDb::getInstance('agent')->setDbData($agent_data)->add();
            if ($agent_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加信贷公司基础信息失败，请联系管理员！");
            }

            //判断名称是否重复
            $agent_admin_where = null;
            $agent_admin_where['phone'] = $phone;
            $agent_admin_count = IDb::getInstance('agent_admin')->setDbWhere($agent_admin_where)->con();
            if ($agent_admin_count > 0) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("您输入的手机号码已经被注册了，请重新输入！");
            }

            //添加代理商负责人
            $agent_admin_data = null;
            $agent_admin_data['agent_id'] = $agent_add;
            $agent_admin_data['pwd'] = md5(md5('88888888'));
            $agent_admin_data['phone'] = $phone;
            $agent_admin_data['real'] = $real;
            $agent_admin_data['is_leader'] = IsLeader::Yes;
            $agent_admin_data['admin_super'] = AdminSuper::Super;
            $agent_admin_data['reg_ip'] = get_client_ip();
            $agent_admin_data['login_count'] = 0;
            $agent_admin_data['reg_time'] = get_date_time();
            $agent_admin_add = IDb::getInstance('agent_admin')->setDbData($agent_admin_data)->add();
            if($agent_admin_add === false){
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加代理商负责人失败！");
            }

            //添加操作日志
            $agent_log_add = ILog::AgentLog($agent_add,get_login_admin_id(),null,"创建信贷公司！");
            if ($agent_log_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加信贷公司基础信息失败，请联系管理员！");
            }
        }else{
            //判断名称是否已经存在
            if(!empty($agent_name)){
                $agent_where = null;
                $agent_where['agent_name'] = $agent_name;
                $agent_info = IDb::getInstance('agent')->setDbWhere($agent_where)->row();
                if (!empty($agent_info)){
                    if ($agent_info['agent_id'] !== intval($agent_id)) {
                        //提示错误信息
                        error("您输入的信贷公司名称已经被使用了！");
                    }
                }
            }

            //添加信贷公司
            $agent_data = null;
            $agent_where = null;
            $agent_where['agent_id'] = $agent_id;
            $agent_data['agent_name'] = $agent_name;
            $agent_data['province_id'] = $province_id;
            $agent_data['city_id'] = $city_id;
            $agent_data['area_id'] = $area_id;
            $agent_data['address'] = $address;
            $agent_data['remark'] = $remark;
            $agent_data['create_time'] = get_date_time();
            $agent_data['is_delete'] = IsDelete::No;
            $agent_upd = IDb::getInstance('agent')->setDbData($agent_data)->setDbWhere($agent_where)->upd();
            if ($agent_upd === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("编辑信贷公司基础信息失败，请联系管理员！");
            }

            //判断名称是否重复
            $agent_admin_where = null;
            $agent_admin_where['phone'] = $phone;
            $agent_admin_info = IDb::getInstance('agent_admin')->setDbWhere($agent_admin_where)->row();
            if (!empty($agent_admin_info)) {
                //判断是否为当前用户
                if(($agent_admin_info['agent_id'] != $agent_id) || ($agent_admin_info['is_leader'] != IsLeader::Yes)){
                    //事物回滚
                    IDb::dbRollback();

                    //提示错误信息
                    error("您输入的手机号码已经被注册了，请重新输入！");
                }
            }

            //添加代理商负责人
            $agent_admin_data = null;
            $agent_admin_where = null;
            $agent_admin_where['agent_id'] = $agent_id;
            $agent_admin_where['is_leader'] = IsLeader::Yes;
            $agent_admin_data['phone'] = $phone;
            $agent_admin_data['real'] = $real;
            $agent_admin_data['login_time'] = get_date_time();
            $agent_admin_add = IDb::getInstance('agent_admin')->setDbWhere($agent_admin_where)->setDbData($agent_admin_data)->upd();
            if($agent_admin_add === false){
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("修改代理商负责人失败！");
            }

            //添加操作日志
            $agent_log_add = ILog::AgentLog($agent_id,get_login_admin_id(),null,"修改信贷公司！");
            if ($agent_log_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("编辑信贷公司基础信息失败，请联系管理员！");
            }

            //清除缓存
            IAgentCache::deleteInfo($agent_id);
        }

        //提交事物处理
        IDb::dbCommit();

        //画面跳转
        $this->success("操作成功！",Url("agent_list"));
    }

    /**
     * @功能：信贷公司日志
     * @开发者：cxl
     * @return string
     */
    public function agent_log(){
        //接收参数
        $agent_id = IRequest::get("agent_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //取得日志
        $agent_log_where['agent_id'] = $agent_id;
        $agent_log_list = IDb::getInstance('agent_log')->setDbWhere($agent_log_where)->setDbOrder("agent_log_id desc")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("agent_log_list",$agent_log_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信贷公司删除
     * @开发者：cxl
     * @return string
     */
    public function agent_del(){
        //接收参数
        $agent_id = IRequest::get("agent_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $agent_where['agent_id'] = $agent_id;
        $agent_data['is_delete'] = IsDelete::Yes;
        $agent_data['update_time'] = get_date_time();
        $agent_upd = IDb::getInstance('agent')->setDbWhere($agent_where)->setDbData($agent_data)->upd();
        if($agent_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("信贷公司禁用失败！");
        }

        //添加操作日志
        $agent_log_add = ILog::AgentLog($agent_id,get_login_admin_id(),null,"信贷公司禁用！");
        if ($agent_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信贷公司禁用失败！");
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        IagentCache::deleteInfo($agent_id);

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：贷经理列表
     * @开发者：cxl
     * @return string
     */
    public function agent_admin(){
        //接收参数
        $agent_id = IRequest::get("agent_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");
        $real = IRequest::get("real");
        $phone = IRequest::get("phone");

        //取得信贷公司负责人
        $agent_admin_where['agent_id'] = $agent_id;
        if(!empty($real)) $agent_admin_where['real'] = array("like","%{$real}%");
        if(!empty($phone)) $agent_admin_where['phone'] = array("like","%{$phone}%");
        $agent_admin_list = IDb::getInstance('agent_admin')->setDbWhere($agent_admin_where)->sel();

        //页面赋值
        $this->assign("agent_id",$agent_id);
        $this->assign("real",$real);
        $this->assign("phone",$phone);
        $this->assign("agent_admin_list",$agent_admin_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：贷经理详情
     * @开发者：cxl
     * @return string
     */
    public function agent_admin_info(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");

        //判断名称是否已经存在
        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_id);
        if(empty($agent_admin_info)){
            error("取得信贷经理详情失败！");
        }

        //设置页码layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("agent_admin_info",$agent_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信贷公司认证
     * @开发者：cxl
     * @return string
     */
    public function agent_authentication(){
        //接收参数
        $agent_id = IRequest::get("agent_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //取得公司信息
        $agent_info = IAgentCache::getInfo($agent_id);

        //判断公司信息是否全面
        if(empty($agent_info['agent_name'])){
            error("未填写信贷公司名称，无法通过企业认证！");
        }

        //判断公司信息是否全面
        if(empty($agent_info['agent_name']) || empty($agent_info['province_id']) || empty($agent_info['city_id']) || empty($agent_info['area_id']) ){
            error("信贷公司地址信息不全，无法通过企业认证！");
        }

        //判断公司信息是否全面
        if(empty($agent_info['business_license_url'])){
            error("信贷公司未上传营业执照，无法通过企业认证！");
        }

        //取得代理商信息
        $agent_admin_where['agent_id'] = $agent_id;
        $agent_admin_where['is_leader'] = IsLeader::Yes;
        $agent_admin_info = IDb::getInstance('agent_admin')->setDbWhere($agent_admin_where)->row();

        //判断公司信息是否全面
        if(empty($agent_admin_info['is_real']) || $agent_admin_info['is_real'] == IsReal::not){
            error("信贷公司负责人为通过实名认证，无法通过企业认证！");
        }

        //页面赋值
        $this->assign("agent_id",$agent_id);
        $this->assign("agent_info",$agent_info);
        $this->assign("agent_admin_info",$agent_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信贷公司认证提交
     * @开发者：cxl
     * @return string
     */
    public function agent_authentication_submit(){
        //接收参数
        $agent_id = IRequest::get("agent_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");
        $is_authentication = IRequest::get("is_authentication",IRequest::NOT_EMPTY,"认证状态不能为空！");
        $authentication_note = IRequest::get("authentication_note");

        //判断审核状态
        if(!in_array($is_authentication,array(IsAuthentication::Yes,IsAuthentication::Fail))){
            error("认证状态错误！");
        }

        //取得公司信息
        $agent_info = IAgentCache::getInfo($agent_id);

        //判断公司信息是否全面
        if(empty($agent_info['agent_name'])){
            error("未填写信贷公司名称，无法通过企业认证！");
        }

        //判断公司信息是否全面
        if(empty($agent_info['agent_name']) || empty($agent_info['province_id']) || empty($agent_info['city_id']) || empty($agent_info['area_id']) || empty($agent_info['address'])){
            error("信贷公司地址信息不全，无法通过企业认证！");
        }

        //判断公司信息是否全面
        if(empty($agent_info['business_license_url'])){
            error("信贷公司未上传营业执照，无法通过企业认证！");
        }

        //取得代理商信息
        $agent_where = null;
        $agent_where['agent_id'] = $agent_id;
        $agent_where['is_leader'] = IsLeader::Yes;
        $agent_admin_info = IDb::getInstance('agent_admin')->setDbWhere($agent_where)->row();
        if(empty($agent_admin_info['is_real']) || $agent_admin_info['is_real'] == IsReal::not){
            error("信贷公司负责人为通过实名认证，无法通过企业认证！");
        }

        IDb::dbStartTrans();

        $agent_admin_upd_where['agent_admin_id'] = $agent_admin_info['agent_admin_id'];

        //判断是否审核通过
        if ($is_authentication === IsAuthentication::Yes){
            $agent_admin_upd_data['is_company'] = IsCompany::Yes;
            $agent_data['is_company'] = IsCompany::Yes;
        }else{
            $agent_admin_upd_data['is_company'] = IsCompany::No;
            $agent_data['is_company'] = IsCompany::No;
        }
        $agent_admin_upd = IDb::getInstance('agent_admin')->setDbData($agent_admin_upd_data)->setDbWhere($agent_where)->upd();
        if ($agent_admin_upd === false){
            //事物回滚
            IDb::dbRollback();

            error('修改信贷经理失败');
        }

        //取得代理商信息
        $agent_where = null;
        $agent_where['agent_id'] = $agent_id;
        $agent_data['is_authentication'] = $is_authentication;
        $agent_data['authentication_note'] = $authentication_note;
        $agent_data['update_time'] = get_date_time();
        $agent_upd = IDb::getInstance('agent')->setDbWhere($agent_where)->setDbData($agent_data)->upd();
        if($agent_upd === false){
            //事物回滚
            IDb::dbRollback();

            error("企业认证失败！");
        }

        //提交事物处理
        IDb::dbCommit();

        //删除缓存
        IAgentCache::deleteInfo($agent_id);
        IAgentAdminCache::deleteInfo($agent_admin_info['agent_admin_id']);

        //画面跳转
        $this->success("操作成功！", Url("agent_list"));
    }

    /**
     * @功能：信贷公司回收站
     * @开发者：cxl
     * @return string
     */
    public function agent_del_list(){
        //接收参数
        $agent_id = IRequest::get('agent_id');
        $agent_name = IRequest::get('agent_name');
        $area = IRequest::get('area');

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }

        //设置查询条件
        $agent_where['ag.is_delete'] = IsDelete::Yes;
        if(!empty($agent_id)) $agent_where['ag.agent_id'] = $agent_id;
        if(!empty($agent_name)) $agent_where['ag.agent_name'] = array("like","%{$agent_name}%");
        if(!empty($province_id)) $agent_where['ag.province_id'] = $province_id;
        if(!empty($city_id)) $agent_where['ag.city_id'] = $city_id;
        $agent_list = IDb::getInstance('agent as ag')
            ->setDbFiled('ag.*,aa.real')
            ->setDbJoin('agent_admin as aa','aa.agent_id = ag.agent_id and aa.is_leader=2','left')
            ->setDbWhere($agent_where)
            ->setDbOrder("ag.agent_id")
            ->setDbGroup("ag.agent_id")
            ->pag();

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //页面赋值
        $this->assign("agent_id",$agent_id);
        $this->assign("agent_name",$agent_name);
        $this->assign("area",$area);
        $this->assign("area_list",$area_list);
        $this->assign("agent_list",$agent_list);

        //渲染模板输出
        return view("agent_del_list");
    }

    /**
     * @功能：信贷公司还原
     * @开发者：cxl
     * @return string
     */
    public function agent_reset(){
        //接收参数
        $agent_id = IRequest::get("agent_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $agent_where['agent_id'] = $agent_id;
        $agent_data['is_delete'] = IsDelete::No;
        $agent_data['update_time'] = get_date_time();
        $agent_upd = IDb::getInstance('agent')->setDbWhere($agent_where)->setDbData($agent_data)->upd();
        if($agent_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信贷公司还原失败！");
        }

        //添加操作日志
        $agent_log_add = ILog::AgentLog($agent_id,get_login_admin_id(),null,"信贷公司还原！");
        if ($agent_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("信贷公司还原失败！");
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        IagentCache::deleteInfo($agent_id);

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：信贷经理列表
     * @开发者：cxl
     * @return string
     */
    public function agent_admin_list(){
        //接收参数
        $agent_admin_id = IRequest::get('agent_admin_id');
        $agent_id = IRequest::get('agent_id');
        $real = IRequest::get('real');
        $nick = IRequest::get('nick');
        $phone = IRequest::get('phone');
        $is_real = IRequest::get('is_real');
        $agent_admin_status = IRequest::get('agent_admin_status');

        //设置查询条件
        $agent_admin_where = null;
        if(!empty($agent_admin_id)) $agent_admin_where['agent_admin_id'] = $agent_admin_id;
        if(!empty($agent_id)) $agent_admin_where['agent_id'] = $agent_id;
        if(!empty($real)) $agent_admin_where['real'] = array("like","%{$real}%");
        if(!empty($nick)) $agent_admin_where['nick'] = array("like","%{$nick}%");
        if(!empty($phone)) $agent_admin_where['phone'] = array("like","%{$phone}%");
        if(!empty($is_real)) $agent_admin_where['is_real'] = $is_real;
        if(!empty($agent_admin_status)) $agent_admin_where['agent_admin_status'] = $agent_admin_status;
        $agent_admin_list = IDb::getInstance('agent_admin')->setDbWhere($agent_admin_where)->setDbOrder("agent_admin_id desc")->pag();

        //页面赋值
        $this->assign("agent_admin_id",$agent_admin_id);
        $this->assign("agent_id",$agent_id);
        $this->assign("real",$real);
        $this->assign("nick",$nick);
        $this->assign("phone",$phone);
        $this->assign("is_real",$is_real);
        $this->assign("agent_admin_status",$agent_admin_status);
        $this->assign("agent_admin_list",$agent_admin_list);

        //渲染模板输出
        return view("agent_admin_list");
    }

    /**
     * @功能：信贷经理推荐增加
     * @开发者：gys
     */
    public function agent_admin_recommend(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");

        //取得经理详情
        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_id);

        //页面赋值
        $this->assign("agent_admin_id",$agent_admin_id);
        $this->assign("agent_admin_info",$agent_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信贷经理推荐提交
     * @开发者：gys
     * @return string
     */
    public function agent_admin_recommend_submit(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");
        $agent_admin_recommend_location = IRequest::get("agent_admin_recommend_location",IRequest::NOT_EMPTY,"推荐位置不能为空！");
        $rate = IRequest::get("rate",IRequest::NUMBER,"评分不能为空！");
        $rate_num = IRequest::get("rate_num",IRequest::NUMBER,"评分人数不能为空！");
        $slogan = IRequest::get("slogan",IRequest::NOT_EMPTY,"服务不能为空！");
        $agent_admin_note = IRequest::get("agent_admin_note");
        $order_by = IRequest::get("order_by");
        $remark = IRequest::get("remark");

        $admin_info = IAgentAdminCache::getInfo($agent_admin_id);
        //判断信贷经理删除状态
        if(empty($admin_info['is_delete']) || $admin_info['is_delete'] == IsDelete::Yes){
            error("信贷经理已被删除不能推荐！");
        }

        //判断信贷经理状态
        if(empty($admin_info['agent_admin_status']) || $admin_info['agent_admin_status'] != AgentAdminStatus::normal){
            error("信贷经理已被禁用不能推荐！");
        }

        //判断信贷经理是否实名
        if(empty($admin_info['is_real']) || $admin_info['is_real'] != IsReal::yes){
            error("信贷经理尚未实名认证不能推荐！");
        }

        //查看是否存在历史数据
        $where = null;
        $where['agent_admin_id'] = $agent_admin_id;
        $where['agent_admin_recommend_location'] = $agent_admin_recommend_location;
        $loan_tag_count = IDb::getInstance('agent_admin_recommend')->setDbWhere($where)->con();
        if($loan_tag_count > 0){
            //修改数据
            $data = null;
            $data['agent_admin_note'] = $agent_admin_note;
            $data['rate'] = $rate;
            $data['rate_num'] = $rate_num;
            $data['slogan'] = $slogan;
            $data['order_by'] = $order_by;
            $data['remark'] = $remark;
            $data['is_delete'] = IsDelete::No;
            $data['create_admin'] = get_login_admin_id();
            $data['create_time'] = get_date_time();
            $loan_tag_upd = IDb::getInstance('agent_admin_recommend')->setDbWhere($where)->setDbData($data)->upd();
            if($loan_tag_upd == false){
                error("推荐失败！");
            }
        }else{
            //修改数据
            $data = null;
            $data['agent_admin_id'] = $agent_admin_id;
            $data['agent_admin_recommend_location'] = $agent_admin_recommend_location;
            $data['agent_admin_note'] = $agent_admin_note;
            $data['rate'] = $rate;
            $data['rate_num'] = $rate_num;
            $data['slogan'] = $slogan;
            $data['order_by'] = $order_by;
            $data['remark'] = $remark;
            $data['update_admin'] = get_login_admin_id();
            $data['update_time'] = get_date_time();
            $loan_tag_add = IDb::getInstance('agent_admin_recommend')->setDbWhere($where)->setDbData($data)->add();
            if($loan_tag_add == false){
                error("推荐失败！");
            }
        }

        //删除缓存
        IAgentAdminRecommendCache::deleteList();

        //画面跳转
        $this->success("操作成功！",Url("agent_admin_list"));
    }

    /**
     * @功能：信贷经理实名认证
     * @开发者：wdd
     */
    public function agent_admin_real(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");

        //取得经理详情
        $agent_admin_info = IAgentAdminCache::getInfo($agent_admin_id);

        //页面赋值
        $this->assign("agent_admin_id",$agent_admin_id);
        $this->assign("agent_admin_info",$agent_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信贷经理实名认证提交
     * @开发者：wdd
     */
    public function agent_admin_real_submit(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");
        $is_authentication = IRequest::get("is_authentication",IRequest::NOT_EMPTY,"审核状态不能为空！");
        $audit_note = IRequest::get("audit_note");

        if($is_authentication == IsAuthentication::Yes){
            $is_real = IsReal::yes;
        }else if ($is_authentication == IsAuthentication::Fail){
            $is_real = IsReal::not;
        }

        $where['agent_admin_id'] = $agent_admin_id;
        $data['is_real'] = $is_real;
        $data['audit_state'] = $is_authentication;
        $data['audit_note'] = $audit_note;
        $agent_admin_upd = IDb::getInstance('agent_admin')->setDbData($data)->setDbWhere($where)->upd();
        if ($agent_admin_upd === false){
            error('认证失败');
        }

        //删除缓存
        IAgentAdminCache::deleteInfo($agent_admin_id);
        IAgentAdminCache::deleteList();
        //画面跳转
        $this->success("操作成功！",Url("agent_admin_list"));
    }

    /**
     * @功能：信贷经理禁用
     * @开发者：wdd
     */
    public function agent_admin_del(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");

        $where['agent_admin_id'] = $agent_admin_id;
        $data['agent_admin_status'] = AgentAdminStatus::blacklist;
        $agent_admin_del = IDb::getInstance('agent_admin')->setDbWhere($where)->setDbData($data)->upd();
        if($agent_admin_del === false){
            error('禁用失败');
        }

        //删除缓存
        IAgentAdminCache::deleteInfo($agent_admin_id);

        //画面跳转
        $this->success("操作成功！",Url("agent_admin_list"));
    }

    /**
     * @功能：信贷经理启用
     * @开发者：wdd
     */
    public function agent_admin_restore(){
        //接收参数
        $agent_admin_id = IRequest::get("agent_admin_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");

        $where['agent_admin_id'] = $agent_admin_id;
        $data['agent_admin_status'] = AgentAdminStatus::normal;
        $agent_admin_del = IDb::getInstance('agent_admin')->setDbWhere($where)->setDbData($data)->upd();
        if($agent_admin_del === false){
            error('启用失败');
        }

        //删除缓存
        IAgentAdminCache::deleteInfo($agent_admin_id);

        //画面跳转
        $this->success("操作成功！",Url("agent_admin_list"));
    }
}